<?php


namespace app\admin\model\management;


use app\common\model\TimeModel;
use app\common\constants\MenuConstant;
class Area extends TimeModel
{
    protected $name = 'area';

  /**
     * @desc 需要处理的数组
     *
     * @var null
     */
    public $array = null;

    /**
     * @desc pid名称
     * @var pid
     */
    public $pidname = 'pid';
    public function getArea(){
        $data=$this->field('id,pid,name')->order('id asc')->select()->toArray();
        $data      = $this->getTreeList($data);
        foreach ($data as $k => $v) {

            if ($v['value'] != 351) {

                unset($data[$k]);

            }

        }
        return $data;
    }
    public function genTree($items,$id='id',$pid='pid',$son = 'children'){
		$tree = array(); //格式化的树
		$tmpMap = array();  //临时扁平数据
		
		foreach ($items as $item) {
			$tmpMap[$item[$id]] = $item;
		}
       
		foreach ($items as $item) {
           
			if (isset($tmpMap[$item[$pid]])) {
				$tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
			} else {
              
				$tree[] = &$tmpMap[$item[$id]];
			}
		}
		return $tree;
    }
    public function getTreeList($array= array(),$pid=0)
    {
        $list = [];
        foreach($array as $value) {
            if($value[$this->pidname]==$pid){
                $value1['value']=$value['id'];
                $value1['label'] = $value['name'];
                $value1['children'] = $this->getTreeList($array,$value['id']);
                if($value1['children'] == null){  
                    unset($value1['children']);            
                }  
                $list[]=$value1;
            } 
        }
        return $list;
    }

}